angular.module('imsApp').controller('wholeDataController', ['$scope', '$state', 'ImsAlert', 'AdDataManager',
    function ($scope, $state, ImsAlert, AdDataManager) {
        $scope.search = {};
        $scope.rangeDate = {
            startDate: null,
            endDate: null,
            onSelected: function (data, scope) {  //时间选择确认事件
                $scope.search.startDay = data.startDate ? moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD") : null;
                $scope.search.endDay = data.endDate ? moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD") : null;
                searchData();
            }
        };

        AdDataManager.wholeDataView().then(function (result) {
            $scope.totalView = result.data.userDataVO;
        })
        searchData();


        $scope.tabSelect = function (index) {
            if (index == 0) {
                setDataShow('11111',xAxis, YtotalUser)

            } if (index == 1) {
                setDataShow('启动次数',xAxis, YStart)

            } if (index == 2) {
                setDataShow('活跃用户',xAxis, YactiveUsers)

            } if (index == 3) {
                setDataShow('播放次数',xAxis, YplayUsers)

            } if (index == 4) {
                setDataShow('广告用户',xAxis, YcumulativeAdUsers)

            }

        };

        var YtotalUser = [];
        var YStart = [];
        var YactiveUsers = [];
        var YplayUsers = [];
        var YcumulativeAdUsers = [];
        var xAxis = [];


        function searchData() {
            AdDataManager.wholeDataViewDetail($scope.search).then(function (result) {
                $scope.list = angular.copy(result.data.userDataVO).reverse();
                xAxis = [];
                var data = result.data.userDataVO;
                YtotalUser = [];
                YStart = [];
                YactiveUsers = [];
                YplayUsers = [];
                YcumulativeAdUsers = [];

                data.forEach(function (item) {
                    xAxis.push(item.dayTime)
                    YtotalUser.push(item.cumulativeUsers)
                    YStart.push(item.startNum)
                    YactiveUsers.push(item.activeUsers)
                    YplayUsers.push(item.playNum)
                    YcumulativeAdUsers.push(item.cumulativeAdUsers)
                })
                $scope.tabSelect(0);

            })

        }


        function setDataShow(name,xAxis, yData) {

            $scope.chartOpt = {
                tooltip: {
                    trigger: 'axis'
                },

                toolbox: {
                    show: true,
                    feature: {
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        data: xAxis,
                        axisLabel: {
                            interval: 0,
                            rotate: 10
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '数量',
                        type: 'line',
                        data: yData,
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    }]
            };

        }

    }])


